
from analyzer.product_sale_data_analyzer import ProductSaleDataAnalyzer
from analyzer.user_login_data_analyzer import UserLoginDataAnalyzer
from analyzer.user_register_data_analyzer import UserRegisterAnalyzer
from x_com import xport_core
from x_com import source_api
import x_com_lib
from x_com.import_api import ecommerce_data_source_service_0_0_1

logger = xport_core.getLogger()

class EcommerceDataAnalysisService:

  # 商品数据分析
  product_sale_data_analyzer: ProductSaleDataAnalyzer
  # 用户登录数据分析
  user_login_data_analyzer: UserLoginDataAnalyzer
  # 用户注册数据分析
  user_register_data_analyzer: UserRegisterAnalyzer
    
  def __init__(self) -> None:
    # 
    self.product_sale_data_analyzer = ProductSaleDataAnalyzer()
    self.user_login_data_analyzer = UserLoginDataAnalyzer()
    self.user_register_data_analyzer = UserRegisterAnalyzer()

  async def on_init(self):
    logger.info("on_init...")

  async def on_finalize(self):
    logger.info("on_finalize...")

  # 订阅数据
  # SubscribeData
  async def subscribe_data(self, ctx: xport_core.Context):
    subscibe_info = x_com_lib.PySubscribeTopicInfo()
    subscibe_info.tag = [] 
    subscibe_info.topic = "ProductSale"
    logger.info(f"节点 订阅 ProductSale 事件...")
    x_com_lib.stream.dxc_subscribe_topic(subscibe_info)
    subscibe_info.topic = "UserRegister"
    logger.info(f"节点 订阅 UserRegister 事件...")
    x_com_lib.stream.dxc_subscribe_topic(subscibe_info)
    subscibe_info.topic = "UserLogin"
    logger.info(f"节点 订阅 UserLogin 事件...")
    x_com_lib.stream.dxc_subscribe_topic(subscibe_info)
    # 
    await ecommerce_data_source_service_0_0_1.subscribe_data()

  # GetProductData
  async def get_product_data(self, ctx: xport_core.Context,  param: source_api.DataRequest) -> source_api.ProductSaleData:
    product_sale_data =  source_api.ProductSaleData()
    year_data = self.product_sale_data_analyzer.get_data_by_year(param.year)
    # 数据为空的时候，直接返回空的数据
    if year_data is None:
      return product_sale_data

    product_sale_data.revenu = year_data.revenu
    product_sale_data.volume = year_data.volume
 

    # 月度销售额数据
    # 填充 12 个月的数据
    #  range 左闭右开的
    for i in range(1, 13):
      # 获取月度销售额数据
      revenu =  year_data.monthly_revenu_dict.get(i)
      # 数据不存在，设置为 
      monthly_revenu =  source_api.MonthlyRevenu()
      monthly_revenu.month = i
      if revenu is None:
        # 没统计到的月份设置为 0
        monthly_revenu.revenu = 0.0
        # 添加数组
      else:
        monthly_revenu.revenu = revenu
      product_sale_data.monthly_revenu_datas.append(monthly_revenu)

      # 获取月度销量
      volume = year_data.monthly_volume_dict.get(i)
      # 数据不存在，设置为 
      monthly_volume =  source_api.MonthlyVolume()
      monthly_volume.month = i
      if revenu is None:
        # 没统计到的月份设置为 0
        monthly_volume.volume = 0
        # 添加数组
      else:
        monthly_volume.volume = volume
      product_sale_data.monthly_volume_datas.append(monthly_volume)

    # 产品分类销售额占比
    for category, share in year_data.product_revenu_share_dict.items():
      share_data = source_api.ProductRevenuShare()
      share_data.category = category
      share_data.share = share
      product_sale_data.product_revenu_share_datas.append(share_data)

    # 产品分类销量占比
    for category, share in year_data.product_volume_share_dict.items():
      share_data = source_api.ProductVolumeShare()
      share_data.category = category
      share_data.share = share
      product_sale_data.product_volume_share_datas.append(share_data)

    return product_sale_data


  # GetUserRegisterData
  async def get_user_register_data(self, ctx: xport_core.Context,  param: source_api.DataRequest) -> source_api.UserRegisterData:
    user_register_data = source_api.UserRegisterData()
    # 总人数
    user_register_data.total_num = self.user_register_data_analyzer.get_total_num()
    year_data = self.user_register_data_analyzer.get_data_by_year(param.year)
    if year_data is None:
      return user_register_data
    # 填充的 12 个月的数据
    for i in range(1, 13):
      monthly_regisetr_data = source_api.MonthlyRegister()
      monthly_regisetr_data.month =  i
      num = year_data.monthly_num_dict.get(i)
      if num is None:
        monthly_regisetr_data.num = 0
      else:
        monthly_regisetr_data.num = num
      user_register_data.monthly_register_datas.append(monthly_regisetr_data)
    return user_register_data

  # GetUserActiveData
  async def get_user_active_data(self, ctx: xport_core.Context,  param: source_api.DataRequest) -> source_api.UserActiveData:
    user_active_data = source_api.UserActiveData()
    user_active_data.online_num = self.user_login_data_analyzer.online_num 
    year_data = self.user_login_data_analyzer.get_data_by_year(param.year)
    if year_data is None:
      return user_active_data
    
    # 填充的 12 个月的数据
    for i in range(1, 13):
      monthly_active_data = source_api.MonthlyActive()
      monthly_active_data.month =  i
      num = year_data.monthly_active_num_dict.get(i)
      if num is None:
        monthly_active_data.num = 0
      else:
        monthly_active_data.num = num
      user_active_data.monthly_active_datas.append(monthly_active_data)
    # 按时间段分布
    for i in range(0, 12):
      hour_active_data = source_api.HourActive()
      hour_active_data.segment = i
      num = year_data.hour_active_num_dict.get(i)
      if num is None:
        hour_active_data.num = 0
      else:
        hour_active_data.num = num
      user_active_data.hour_active_datas.append(hour_active_data)
    return user_active_data